<?php
namespace jy_jd;
defined('BY_JYA') or exit('error');
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_jd/model')->方法名();
*/
class model extends \plugin{
	public function get($params,&$total=null,$lat_lng=array()){
		if(!$params['orderby']){
			$params['orderby']=array('id desc');
		}
		$list=pdo_getall('jy_jd_hotel',$params['params'],$params['fields']);
		foreach($list as $k=>&$v){
			$v['image']=$v['image']?tomedia($v['image']):'';
			$v['lat_lng']=json_decode($v['lat_lng'],true);
			if($v['lat_lng']['lng'] && $v['lat_lng']['lat']){
				$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
				$v['juli']=intval($v['juli']/1000);
			}else{
				if($lat_lng){
					unset($list[$k]);
				}
			}
		}
		paixu($list,'juli','asc');
		return $list;
	}
	public function get_innum($room,$intime='',$day=1){
		$day=$day?:1;
		$intime=$intime?:date("Y-m-d");
		$time=strtotime($intime);
		for($i=0;$i<$day;$i++){
			$nowday=date("Y-m-d",$time+$i*24*3600);
			$innum=pdo_getsum('jy_jd_order',array('allday like'=>'%'.$nowday.'%','status'=>array(0,1,2,3,6),'hotelid'=>$room['hotelid'],'roomid'=>$room['id']),'num');
			return $innum;
		}
		return $innum;
	}
	public function check_innum($room,$intime='',$day=1,$num=1){
		$num=$num?:1;
		$day=$day?:1;
		$intime=$intime?:date("Y-m-d");
		$time=strtotime($intime);
		for($i=0;$i<$day;$i++){
			$nowday=date("Y-m-d",$time+$i*24*3600);
			$roomdaynum=$this->getroomdaynum($room,$nowday);
			$innum=pdo_getsum('jy_jd_order',array('allday like'=>'%'.$nowday.'%','status'=>array(0,1,2,3,6),'hotelid'=>$room['hotelid'],'roomid'=>$room['id']),'num');
			if($roomdaynum-$innum<$num){
				exi('该房型'.$nowday.'日期数量不足,请选择其他房型','error');
			}
		}
		return $innum;
	}
	public function getroomdaynum($room,$nowday=''){
		$nowday=$nowday?:date("Y-m-d");
		$num=pdo_getcolumn('jy_jd_room_price',array('roomid'=>$room['id'],'onday'=>$nowday,'uniacid'=>$_SESSION['uniacid']),'num');
		$num=$num?:$room['num'];
		return $num;
	}
	public function taskuporder(){
		$nobuy_time=$this->setting['nobuy_time'];
		$nobuy_time=$nobuy_time?$nobuy_time:15;
		$time=time()-$nobuy_time*60;
		pdo_update('jy_jd_order',array('status'=>7),array('uniacid'=>$_SESSION['uniacid'],'status'=>0,'ispay'=>0,'createtime <='=>$time));
		return true;
	}
	public function uporderstatus($order,$status=0){
		if(!$order['status'] && !$order['ispay'] && $order['createtime']+15*60<time()){
			pdo_update('jy_jd_order',array('status'=>7),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']));
		}
		return true;
	}
	public function get_minprice($taocan){
		foreach($taocan as $item){
			for($i=0;$i<=6;$i++){
				$minprice=$minprice?:$item['price'.$i];
				if($item['price'.$i] && $item['price'.$i]<$minprice){
					$minprice=$item['price'.$i];
				}
			}
		}
		return $minprice;
	}
	public function get_nowprice($room=array(),$intime=''){
		$intime=$intime?:date("Y-m-d");
		$dayprice=pdo_getcolumn('jy_jd_room_price',array('roomid'=>$room['id'],'onday'=>$intime,'uniacid'=>$_SESSION['uniacid']),'price');
		$price=$dayprice?$dayprice:$room['price'];
		return $price;
	}
	public function get_uptaocan($room=array(),$intime=''){
		$intime=$intime?:date("Y-m-d");
		$dayprice=pdo_getcolumn('jy_jd_room_price',array('taocankey'=>0,'roomid'=>$room['id'],'onday'=>$intime,'uniacid'=>$_SESSION['uniacid']),'price');
		$price=$dayprice?$dayprice:$room['price'];
		$taocan=$room['taocan'];
		if(count($taocan)<=0){
			$taocan[]=array('title'=>'标准房');
		}
		foreach($taocan as $key=>&$item){
			$item['key']=$key;
			$minprice=0;
			for($i=0;$i<=6;$i++){
				$minprice=$minprice?:$item['price'.$i];
				if($item['price'.$i] && $item['price'.$i]<$minprice){
					$minprice=$item['price'.$i];
				}
				$item['nowprices'][$i]=$item['price'.$i];
			}
			$dayprice=pdo_getcolumn('jy_jd_room_price',array('taocankey'=>$key,'roomid'=>$room['id'],'onday'=>$intime,'uniacid'=>$_SESSION['uniacid']),'price');
			$item['price']=$item['price']?:$price;
			$item['price']=$dayprice?:$item['price'];
		}
		return $taocan;
	}
	public function get_upprice($roomid,$intime=0,$tkey=0,$num=1,$day=1,$tprice=0){
		$room=pdo_get('jy_jd_room',array('id'=>$roomid,'uniacid'=>$_SESSION['uniacid']));
		$room['taocan']=json_decode($room['taocan'],true);
		$room['taocan']=$this->get_uptaocan($room,$intime);
		$room['price']=$room['taocan'][$tkey]['price'];
		$intime=$intime?strtotime($intime):time();
		$price=0;
		for($i=0;$i<$day;$i++){
			$onday=date("Y-m-d",$intime+$i*24*3600);
			$room_price=pdo_get('jy_jd_room_price',array('taocankey'=>$tkey,'onday'=>$onday,'roomid'=>$roomid,'uniacid'=>$_SESSION['uniacid']));
			$price +=$room_price['price']>0?$room_price['price']:$room['price'];
		}
		$yaprice=$num*$room['iprice'];
		$allprice=$num*$price+$yaprice;
		$res=array('num'=>$num,'allprice'=>$allprice,'price'=>$price,'yaprice'=>$yaprice);
		return $res;
	}
	public function get_uppriceold($roomid,$intime=0,$tkey=0,$num=1,$day=1){
		$room=pdo_get('jy_jd_room',array('id'=>$roomid));
		$room['taocan']=json_decode($room['taocan'],true);
		$intime=$intime?strtotime($intime):time();
		$price=0;
		for($i=0;$i<$day;$i++){
			$weekid=date("w",$intime+$i*24*3600);
			$price +=$room['taocan'][$tkey]['price'.$weekid];
		}
		$yaprice=$num*$room['iprice'];
		$allprice=$num*$price+$yaprice;
		$res=array('num'=>$num,'allprice'=>$allprice,'price'=>$price,'yaprice'=>$yaprice);
		return $res;
	}
	public function gethotel($parameter,$limit=5,$lat_lng=array(),$page=1,$limitjuli=0){
		$limitjuli=intval($limitjuli);
		if($lat_lng['lat'] || $lat_lng['lng']){
			$lat_lng=array('lat'=>$lat_lng['lat'],'lng'=>$lat_lng['lng']);
			$this->session('lat_lng',$lat_lng);
		}
		$lat_lng=$this->session('lat_lng');
		$total='';
		$parameter['orderby']=$parameter['orderby']?:'paixu desc,id desc';
		$list=pdo_getslice('jy_jd_hotel',$parameter['params'],$parameter['limit'],$total,$parameter['fields'],$parameter['key'],$parameter['orderby']);
		foreach($list as $k=>&$v){
			$v['pcd']=json_decode($v['pcd'],true);
			$v['lat_lng']=json_decode($v['lat_lng'],true);
			$v['lat_lng']['lng']=$v['lat_lng']['lng']?:'114.424632';
			$v['lat_lng']['lat']=$v['lat_lng']['lat']?:'23.126942';
			$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
			$v['juli']=intval($v['juli']/1000);
			if($limitjuli && $v['juli']>$limitjuli){
				unset($list[$k]);
			}
			$v['paixunew']=$v['juli'];
			if($parameter['orderbykey']=='paixu'){
				if($v['paixu']){
					$v['paixunew']=1/$v['paixu'];
				}elseif($v['tuijie']){
					$v['paixunew']=$v['juli']>120?$v['juli']-120:$v['juli'];
					$paixu=$v['star']?5/$v['star']:5;
					$v['paixunew'] +=$paixu;
				}elseif($v['star']){
					$v['paixunew']=$v['juli']>60?$v['juli']-60:$v['juli'];
					$paixu=5/$v['star'];
					$v['paixunew'] +=$paixu;
				}else{
					$v['paixunew']=$v['juli']>60?$v['juli']-60:$v['juli'];
					$v['paixunew'] +=5;
				}
			}
			if($parameter['orderbykey']=='tuijie' && $v['tuijie']){
				$v['paixunew']=$v['juli']>60?$v['juli']-60:0;
			}
			if($parameter['orderbykey']=='star' && $v['star']>0){
				$v['paixunew']=$v['juli']>60?$v['juli']-60:0;
				$paixu=5/$v['star'];
				$v['paixunew']=$v['paixu']+$paixu;
			}
		}
		paixu($list,'paixunew','asc');
		if($limit){
			$list=array_slice($list,0,intval($limit));
		}else{
			$page=$page?:1;
			$start=($page-1)*10;
			$list=array_slice($list,$start,10);
		}
		return $list;
	}
	public function notice_shenhe($merch,$ispass=0,$wxapp_id=0){
		//申请结果通知
		$template_id=cfc('account')->get_tpl_template_id('OPENTM412123809','申请结果通知');
		$wxapp_id=$wxapp_id?:$_SESSION['wxapp_id'];
		if($wxapp_id){
			$wxapptemplate_id=cfc('wxapp_api',$wxapp_id)->get_tpl_template_id('10415',[5,4,2],'shenhe','审核结果通知','jy_merch');
		}
		$message=array(
			'first'=>array('value'=>'您好！您申请已处理完成。',"color" => "#4a5077"),
			'keyword1'=>array('value'=>'店铺《'.$merch['merchname'].'》的申请！',"color" => "#4a5077"),
			'keyword2'=>array('value'=>date('Y-m-d H:i:s',$merch['applytime']),"color" => "#4a5077"),
			'keyword5'=>array('value'=>$ispass?'审核通过':'审核失败',"color" => "#4a5077"),
			'remark'=>array('value'=>'感谢您的使用，点击查看详情！',"color" => "#4a5077"),
		);
		$wxappmsg=array(
			array('value'=>'店铺《'.$merch['merchname'].'》的申请！'),
			array('value'=>date('Y-m-d H:i:s',time())),
			array('value'=>$ispass?'审核通过':'审核失败'),
		);
		if(!$url){
			$url=custom_url('jy_merch/manage.index/',true);
		}
		cfc('account')->sendtwoNotice($merch['mid'],$template_id,$message,$url,'shenhe','jy_merch',$merch['id']);
		if($wxapp_id){
			$url='/jy_merch/manage/index/index';
			$template_id=cfc('wxapp_api',$wxapp_id)->checktplNotice($merch['mid'],$wxapptemplate_id,$wxappmsg,$url,'shenhe','jy_merch',$merch['id']);
		}
	}
	public function count_comments($hotelid){
		$percent = 100;
		$count = array(
			'all'=>$this->pdo_count('comments',array('hotelid'=>$goodsid,'status'=>1)),
			'good'=>$this->pdo_count('comments',array('hotelid'=>$goodsid,'status'=>1,'star'=>5)),
			'normal'=>$this->pdo_count('comments',array('hotelid'=>$goodsid,'status'=>1,'star between'=>array(2,4))),
			'bad'=>$this->pdo_count('comments',array('hotelid'=>$goodsid,'status'=>1,'star'=>array(0,1))),
			'pic'=>$this->pdo_count('comments',array('hotelid'=>$goodsid,'status'=>1,'images !='=>'')),
		);
		if(0 < $count['all']){
			$percent = intval(($count['good'] / $count['all']) * 100);
			$normal_percent= intval(($count['normal'] / $count['all']) * 100);
			$bad_percent= intval(($count['bad'] / $count['all']) * 100);
			$pic= intval(($count['pic'] / $count['all']) * 100);
		}
		return array(
			"count" => $count,
			'percent' => $percent,
			'normal_percent'=>$normal_percent,
			'bad_percent'=>$bad_percent,
			'pic'=>$pic
		);
	}
	public function upmember($hotelid=0,$name='',$mobile=''){
		$member=pdo_get('jy_jd_member',array('hotelid'=>$hotelid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($member['id']){
			return false;
		}
		$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'hotelid'=>$hotelid,
			'mid'=>$_SESSION['uid'],
			'realname'=>$name?:$member['realname'],
			'nickname'=>$member['nickname'],
			'avatar'=>$member['avatar'],
			'sex'=>$member['sex'],
			'mobile'=>$mobile?:$member['mobile'],
			'createtime'=>TIMESTAMP		
		);
		pdo_insert('jy_jd_member',$data);
		return true;
	}
}